package com.fjl.test;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zondy.mapgis.android.authorize.Authorize;
import com.zondy.mapgis.android.internal.utils.ToastUtil;
import com.zondy.mapgis.android.internal.utils.ViewIDUtil;
import com.zondy.mapgis.android.utils.BitmapCreator;
import com.zondy.mapgis.android.utils.NinePatchTool;
import com.zondy.mapgis.android.utils.StateListDrawableCreator;

public class AuthorizeationForUserDialog extends Dialog
{
	private LinearLayout mRootLinearLayout;
	private DisplayMetrics mDisplayMetrics = null;

	private float dp2px(float dp)
	{
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mDisplayMetrics);
	}

	public AuthorizeationForUserDialog(final Context context, String strMsg)
	{
		super(context);
		this.setCancelable(false);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		final Authorize authorize = Authorize.getInstance(context);

		mDisplayMetrics = context.getResources().getDisplayMetrics();
		LayoutParams fillParentLinearLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		LayoutParams customLinearLayoutParams;

		// 根视图
		mRootLinearLayout = new LinearLayout(context);
//		mRootLinearLayout.setLayoutParams(fillParentLinearLayoutParams);
		customLinearLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		mRootLinearLayout.setLayoutParams(customLinearLayoutParams);
		mRootLinearLayout.setOrientation(LinearLayout.VERTICAL);
		mRootLinearLayout.setBackgroundColor(Color.parseColor("#f4f4f4"));

		// top视图
		LinearLayout topLinearLayout = new LinearLayout(context);
		BitmapDrawable topBgDrawable = new BitmapDrawable(BitmapCreator.fromAsset(context, "res/drawable/mobile_topbg.png", 1.0f));

		topLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, (int) dp2px(50)));
		topLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
		topLinearLayout.setGravity(Gravity.CENTER_VERTICAL);
		topLinearLayout.setBackgroundColor(Color.BLUE);

		Drawable normalBtnDrawable = null;
		Drawable pressedBtnDrawable = null;
		Drawable disableBtnDrawable = null;

		try
		{
			normalBtnDrawable = NinePatchTool.decodeDrawableFromAsset(context, "res/drawable/mobile_button.9.png");
			pressedBtnDrawable = NinePatchTool.decodeDrawableFromAsset(context, "res/drawable/mobile_button_on.9.png");
			disableBtnDrawable = NinePatchTool.decodeDrawableFromAsset(context, "res/drawable/mobile_button_disable.9.png");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		Button qrCodeBtn = new Button(context);
		StateListDrawable qrCodeBtnDrawable = StateListDrawableCreator.fromDrawables(normalBtnDrawable, pressedBtnDrawable, disableBtnDrawable);

		customLinearLayoutParams = new LayoutParams((int) dp2px(120), (int) dp2px(30.5f));
		customLinearLayoutParams.setMargins((int) dp2px(10), 0, 0, 0);

		qrCodeBtn.setText("二维码认证");
		qrCodeBtn.setLayoutParams(customLinearLayoutParams);
		qrCodeBtn.setTextColor(Color.WHITE);
		qrCodeBtn.setBackgroundDrawable(qrCodeBtnDrawable);
		qrCodeBtn.setId(ViewIDUtil.generateViewId());

		topLinearLayout.addView(qrCodeBtn);

		// 中间提示
		TextView tipTextView = new TextView(context);

		customLinearLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, (int) dp2px(50));
		customLinearLayoutParams.weight = 1.0f;
		tipTextView.setLayoutParams(customLinearLayoutParams);
		tipTextView.setGravity(Gravity.CENTER);
		tipTextView.setText("授权");
		tipTextView.setTextColor(Color.WHITE);
		tipTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);

		topLinearLayout.addView(tipTextView);

		// 右侧登录获取
		final Button authorizeBtn = new Button(context);
		StateListDrawable authorizeBtnDrawable = StateListDrawableCreator.fromDrawables(normalBtnDrawable.getConstantState().newDrawable(), pressedBtnDrawable
				.getConstantState().newDrawable(), disableBtnDrawable.getConstantState().newDrawable());

		customLinearLayoutParams = new LayoutParams((int) dp2px(100), (int) dp2px(30.5f));
		customLinearLayoutParams.setMargins(0, 0, (int) dp2px(10), 0);

		authorizeBtn.setText("网络认证");
		authorizeBtn.setLayoutParams(customLinearLayoutParams);
		authorizeBtn.setTextColor(Color.WHITE);
		authorizeBtn.setBackgroundDrawable(authorizeBtnDrawable);

		topLinearLayout.addView(authorizeBtn);

		mRootLinearLayout.addView(topLinearLayout);

		// 帮助
		TextView helpTextView = new TextView(context);

		customLinearLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		customLinearLayoutParams.setMargins((int) dp2px(10), (int) dp2px(10), (int) dp2px(10), 0);
		customLinearLayoutParams.weight = 1.0f;
		customLinearLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;

		helpTextView.setLayoutParams(customLinearLayoutParams);
		helpTextView.setText(strMsg);
		helpTextView.setGravity(Gravity.CENTER);
		helpTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
		helpTextView.setMovementMethod(LinkMovementMethod.getInstance());
		helpTextView.setTextColor(Color.parseColor("#29981A"));

		mRootLinearLayout.addView(helpTextView);

		// 二维码视图
		ImageView qrcodeImageView = new ImageView(context);
		int qrcodeImageSize = (int) dp2px(160);

		customLinearLayoutParams = new LayoutParams(qrcodeImageSize, qrcodeImageSize);
		customLinearLayoutParams.setMargins((int) dp2px(10), (int) dp2px(10), (int) dp2px(10), (int) dp2px(10));
		customLinearLayoutParams.weight = 1.0f;
		customLinearLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;

		qrcodeImageView.setLayoutParams(customLinearLayoutParams);
		qrcodeImageView.setScaleType(ScaleType.FIT_CENTER);

		final String strRegCode = authorize.generateRegCode();
		Bitmap qrcodeImage = authorize.getAuthorizeQRCode(strRegCode, qrcodeImageSize);

		qrcodeImageView.setImageBitmap(qrcodeImage);

		mRootLinearLayout.addView(qrcodeImageView);

		// 认证码视图
		RelativeLayout identityCodeRelativeLayout = new RelativeLayout(context);

		Drawable normalPreferenceDrawable = null;
		Drawable pressedPreferenceDrawable = null;
		try
		{
			normalPreferenceDrawable = NinePatchTool.decodeDrawableFromAsset(context, "res/drawable/mobile_ic_preference_single_normal.9.png");
			pressedPreferenceDrawable = NinePatchTool.decodeDrawableFromAsset(context, "res/drawable/mobile_ic_preference_single_pressed.9.png");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		StateListDrawable preferenceDrawable = StateListDrawableCreator.fromDrawables(normalPreferenceDrawable, pressedPreferenceDrawable,
				normalPreferenceDrawable);

		customLinearLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		customLinearLayoutParams.setMargins((int) dp2px(5), 0, (int) dp2px(5), (int) dp2px(10));
		identityCodeRelativeLayout.setLayoutParams(customLinearLayoutParams);
		identityCodeRelativeLayout.setClickable(true);
		identityCodeRelativeLayout.setGravity(Gravity.CENTER_VERTICAL);
		identityCodeRelativeLayout.setBackgroundDrawable(preferenceDrawable);

		// 认证码编辑框
		final EditText identityCodeEditText = new EditText(context);

		RelativeLayout.LayoutParams customRelativeLayoutParams = new RelativeLayout.LayoutParams((int) dp2px(420), LayoutParams.WRAP_CONTENT);
		customRelativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
		identityCodeEditText.setLayoutParams(customRelativeLayoutParams);
		identityCodeEditText.setHint("认证码");
		identityCodeEditText.setGravity(Gravity.CENTER);
		identityCodeEditText.setPadding(0, (int) dp2px(10), 0, (int) dp2px(10));
		identityCodeEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
		identityCodeEditText.setBackgroundDrawable(null);

		identityCodeRelativeLayout.addView(identityCodeEditText);

		mRootLinearLayout.addView(identityCodeRelativeLayout);

		authorizeBtn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				
			}
		});

		qrCodeBtn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String strCode = identityCodeEditText.getText().toString();

				if (strCode.length() == 0)
				{
					ToastUtil.showToast(context, "请输入扫描二维码后获得的认证码");
					return;
				}

			}
		});
	}
	/**
	 * 设置AlertDialog的大小
	 * 
	 * @param dialog
	 * @param width
	 * @param height
	 */
	public static void setParams(AlertDialog dialog, int width, int height)
	{
		WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
		params.width = width;
		params.height = height;
		dialog.getWindow().setAttributes(params);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(mRootLinearLayout);
	}
}
